package com.poetry.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.poetry.domain.dto.LoginDTO;
import com.poetry.domain.dto.PoemDTO;
import com.poetry.domain.dto.UserDTO;
import com.poetry.domain.po.Admin;
import com.poetry.domain.po.Author;
import com.poetry.domain.po.Poem;
import com.poetry.exception.R;
import com.poetry.service.IAdminService;
import com.poetry.service.IAuthorService;
import com.poetry.service.IPoemService;
import com.poetry.util.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController("/admin")
@Tag(name = "管理员账号管理模块")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @Resource
    private IPoemService poemService;
    @Resource
    private IAuthorService authorService;

    @Operation(summary = "登录")
    @PostMapping("/admin_login")
    public R<String> login(LoginDTO loginDTO) {
        return adminService.login(loginDTO);
    }

    @Operation(summary = "发送验证码")
    @PostMapping("/admin_code")
    public R<String> sendCode(@RequestBody LoginDTO loginDTO) {
        return adminService.sendVerificationCode(loginDTO);
    }

    @Operation(summary = "重置密码")
    @PostMapping("/reset_admin_password")
    public R<String> resetPassword(@RequestBody LoginDTO loginDTO) {
        return adminService.resetPassword(loginDTO);
    }

    @Operation(summary = "设置用户名")
    @PostMapping("/set_admin_message")
    public R<Admin> updateMessage(@RequestBody UserDTO userDTO) {
        return adminService.updateMessage(userDTO);
    }

    @Operation(summary = "获取登录用户信息")
    @GetMapping("/get_info")
    public R<UserDTO> getUserInfo() {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userDTO, adminService.getById(UserContext.getUserId()));
        return R.success("获取用户信息成功", userDTO);
    }

    @Operation(summary = "注销用户")
    @DeleteMapping("/delete_admin")
    public R<String> deleteUser() {
        adminService.removeById(UserContext.getUserId());
        return R.success(null, "注销用户成功");
    }

    @Operation(summary = "创建管理员账号")
    @PostMapping("/create_admin")
    public R<String> createAdmin(@RequestBody LoginDTO loginDTO) {
        return adminService.createAdmin(loginDTO);
    }

    @Operation(summary = "添加作者信息")
    @PostMapping("/add_author")
    public R<String> addAuthor(@RequestBody Author author) {
        return authorService.addAuthor(author);
    }

    @Operation(summary = "更新作者信息")
    @PostMapping("/update_author")
    public R<Author> updateAuthor(@RequestBody Author author) {
        return authorService.updateAuthor(author);
    }

    @Operation(summary = "批量删除作者信息")
    @DeleteMapping("/delete_authors")
    public R<String> deleteAuthorBatch(@RequestBody List<Integer> ids) {
        return authorService.deleteBatchByIds(ids);
    }

    @Operation(summary = "添加诗词信息")
    @PostMapping("/add_poem")
    public R<String> addPoem(@RequestBody Poem poem) {
        return poemService.addPoem(poem);
    }

    @Operation(summary = "更新诗词信息")
    @PostMapping("/update_poem")
    public R<Poem> updatePoem(@RequestBody Poem poem) {
        return poemService.updatePoem(poem);
    }

    @Operation(summary = "批量删除作者信息")
    @DeleteMapping("/delete_poetry")
    public R<String> deletePoemBatch(@RequestBody List<Integer> ids) {
        return poemService.deleteBatchByIds(ids);
    }

    @Operation(summary = "设置诗词类型")
    @PostMapping("/set_category")
    public R<Poem> setCategoryById(@RequestBody PoemDTO poemDTO) {
        return poemService.setCategory(poemDTO);
    }

    @Operation(summary = "文件上传")
    @PostMapping("/admin_upload")
    public R<String> uploadFile(MultipartFile file) {
        return adminService.uploadFile(file);
    }

}
